<?php
import("@.ORG.Url");
class GroupAction extends Action {
    public function index()
    {
        $mGroup = M("group")->field("`id`,`name`,`mark`,`is_lock`")->order("id desc")->select();
        $this->assign('mGroup', $mGroup);
        $this->display();
    }

    /**
     * 创建分组
     */
    public function create()
    {
        $id = $this->_get('id');
        $aGroup = M('group');
        $mResult = array();

        if (!empty($id)) {
            $mResult = $aGroup->field('`id`,`name`,`mark`')->where('id=' . $id)->find();
        }

        if ($this->ispost()) {
            $name = $this->_post('name');
            $mark = $this->_post('mark');
            $groupId = $this->_post('groupId');

            if (!empty($name) && !empty($mark)) {
                $data = array(
                    'name' => $name,
                    'mark' => $mark,
                );

                $result  = array('success' => 0);
                $data['id'] = $groupId;

                if ($groupId) {
                    $r = $aGroup->save($data);
                } else {
                     $r = $aGroup->add($data);
                }

                if ($r >= 0) {
                    $result = array(
                        'success' => 1,
                        'alert' => '操作分组成功!',
                    );
                } else {
                    $result = array(
                    'alert' => '操作分组失败',
                );
                }
            } else {
                $result = array(
                    'alert' => '分组和分组备注不能为空！',
                );
            }

            $this->ajaxReturn($result, '', 1, 'json');
        }

        $this->assign('mResult', $mResult);
        $this->display();
    }

    /**
     * 修改分组
     */
    public function modify()
    {
        $this->display();
    }

    /**
     * 标记群组
     */
    public function lockGroup() {
        $id = $this->_get('id');
        if (empty($id)) {
            $this->error('参数类型失败，请与管理员联系');
        }

        $mGroup = M('group');
        $aResult =  $mGroup->field('`id`,`is_lock`')->where('id=' . $id)->find();

        if (!empty($aResult)) {
            $data['id'] = $id;

            if ($aResult['is_lock'] == GroupModel::UNLOCK_GROUP) {
                $data['is_lock'] = GroupModel::LOCK_GROUP;
            } else if ($aResult['is_lock'] == GroupModel::LOCK_GROUP) {
                $data['is_lock'] = GroupModel::UNLOCK_GROUP;
            }

            if ($mGroup->save($data)) {
               $this->success('操纵用户成功');
            } else {
                $this->error('操作群组不存在');
            }
        }

        $this->display('Group/index');
    }

    /**
     * 分组权限
     */
    public function groupRule()
    {
        $id = $this->_get('id');

        if (empty($id)) {
            $this->error('参数类型失败，请与管理员联系');
        }

        $rule = M('group')->field('rule')->where('id='.$id)->find();

        if ($this->ispost()) {
            unset($_POST['__hash__']);
            $rule = implode(',', $_POST);
            $r = M('group')->save(array('id' => $id, 'rule' => $rule));

            if ($r >= 0) {
                $this->success('权限分配成功！');
            } else {
                $this->error('权限分配失败，请与管理员联系！');
            }
        }

        $this->assign('rule', $rule['rule']);
        $this->display();
    }
}